package cn.hs.ph.dao.impl;

import cn.hs.ph.dao.StationDao;
import cn.hs.ph.entity.Station;
import cn.hs.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class StationDaoImpl implements StationDao {
    /*
    * courierStationAddress: "啊啊"
    * courierStationCity: "长治市"
    * courierStationCommunityId: 1069304
    * courierStationDistrict: "长治县"
    * courierStationImage: ""
    * courierStationName: "啊啊"
    * courierStationProvince: "山西省"
    * */
    /*
    * addTime: "2025-08-12 19:13:54"
    * courierStationAddress: "121111"
    * courierStationCity: "北京市"
    * courierStationCommunityId: 1069338
    * courierStationCommunityName: "11"
    * courierStationDistrict: "西城区"
    * courierStationId: 1011259
    * courierStationImage: "@/assets/shibai.png"
    * courierStationName: "112333444、发货单号11133355557777"
    * courierStationOrderCount: 0
    * courierStationProvince: "北京"
    * courierStationSort: 1
    * courierStationStatus: 0
    * */
    @Override
    public int update(Station station) {
        String sql = "update station set courierStationOrderCount=?,courierStationSort=?,courierStationStatus=?,courierStationCommunityName=?,courierStationAddress=?,courierStationCity=?,courierStationCommunityId=?,courierStationImage=?,courierStationName=?,courierStationProvince=?,addTime=?,courierStationDistrict=? where courierStationId=?";
        Object[] obj = {
                station.getCourierStationOrderCount(),
                station.getCourierStationSort(),
                station.getCourierStationStatus(),
                station.getCourierStationCommunityName(),
                station.getCourierStationAddress(),
                station.getCourierStationCity(),
                station.getCourierStationCommunityId(),
                station.getCourierStationImage(),
                station.getCourierStationName(),
                station.getCourierStationProvince(),
                new Date(),
                station.getCourierStationDistrict(),
                station.getCourierStationId()
        };
        return JDBCDruidUtil.update(sql, obj);
    }

    @Override
    public int delete(int courierStationId) {
         String sql = "delete from station where courierStationId=? ";
         Object[] obj = {courierStationId};
         return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public int insert(Station station) {
        String sql = "insert into station (courierStationId,courierStationName,courierStationImage,courierStationProvince,courierStationCity,courierStationDistrict,courierStationAddress,addTime,courierStationCommunityId,courierStationCommunityName,courierStationOrderCount,courierStationSort,courierStationStatus) values (null,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object[] obj = {
                station.getCourierStationName(),
                station.getCourierStationImage(),
                station.getCourierStationProvince(),
                station.getCourierStationCity(),
                station.getCourierStationDistrict(),
                station.getCourierStationAddress(),
                new Date(),
                station.getCourierStationCommunityId(),
                station.getCourierStationCommunityName(),
                station.getCourierStationOrderCount(),
                station.getCourierStationSort(),
                station.getCourierStationStatus()
        };
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public List<Station> selectAll(Map<String, String> map) {
        List<Station> list = new ArrayList<>();
        String sql = "select * from station where 1=1 ";
        List<Object> params = new ArrayList<>();
        String courierStationName = map.get("courierStationName");
        if(courierStationName!=null && !"".equals(courierStationName)){
            sql+=" and courierStationName like ?";
            params.add("%"+courierStationName+"%");
        }
        String courierStationCommunityName = map.get("communityName");
        if (courierStationCommunityName!=null && !"".equals(courierStationCommunityName)){
            sql+=" and courierStationCommunityName like ?";
            params.add("%"+courierStationCommunityName+"%");
        }
        String stationStatus = map.get("courierStationStatus");
        if(stationStatus!=null && !"".equals(stationStatus)){
            sql += " and courierStationStatus=? ";
            params.add(stationStatus);
        }
        String startTime = map.get("startTime");
        if(startTime!=null && !"".equals(startTime)){
            sql+=" and addTime between ? and ?";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        String num = map.get("pageNum");
        if (num!=null && !"".equals(num)){
            sql+=" limit ?,?";
            int pageNum = Integer.parseInt(num);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add( (pageNum-1)*pageSize );
            params.add(pageSize);
        }
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true){
            try {
                if (!res.next()) break;
                Station station = new Station(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(9),
                        res.getString(11),
                        res.getString(5),
                        res.getString(8),
                        res.getString(4),
                        res.getString(3),
                        res.getInt(6),
                        res.getString(7),
                        res.getInt(10),
                        res.getInt(12),
                        res.getInt(13)
                );
                list.add(station);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        int total = 0;
        String sql = "select count(1) from station where 1=1 ";
        List<Object> params = new ArrayList<>();
        String courierStationName = map.get("courierStationName");
        if(courierStationName!=null && !"".equals(courierStationName)){
            sql+=" and courierStationName like ? ";
            params.add("%"+courierStationName+"%");
        }
        String communityName = map.get("communityName");
        if (communityName!=null && !"".equals(communityName)){
            sql+=" and courierStationCommunityName like ? ";
            params.add("%"+communityName+"%");
        }
        String stationStatus = map.get("courierStationStatus");
        if(stationStatus!=null && !"".equals(stationStatus)){
            sql += " and courierStationStatus=? ";
            params.add(stationStatus);
        }
        String startTime = map.get("startTime");
        if(startTime!=null && !"".equals(startTime)){
            sql+=" and addTime between ? and ? ";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true){
            try {
                if (!res.next()) break;
                total = res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        JDBCDruidUtil.close();
        return total;
    }
}
